package com.lanou.sfu.songforu.network;



import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;

/**
 * 网络状态广播接收器
 * 
 * @author zihao
 * 
 */
public class NetState extends BroadcastReceiver {

	public static boolean IS_ENABLE = true;
	private NetworkInfo gprs;
	private NetworkInfo wifi;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		// 监听网络状态
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		startActivity(context);
	}

	/**
	 * 跳转
	 * 
	 * @param context
	 */
	private void startActivity(Context context) {
		if (!gprs.isConnected() && !wifi.isConnected() && IS_ENABLE) {
			System.out.println("start");
			IS_ENABLE = false;
			((MainActivity) context).showTips(R.mipmap.tips_error,
					"网络未连接，请先连接网络...");
			Intent intent = new Intent().setClass(context,
					NetWorkActivity.class);
			((Activity) context).startActivityForResult(intent, 1);
		}
	}

}